/*
 * @(#)FeedAction.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.action.feed;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.hangame.mbl.action.SmartCommonBaseAction;
import com.hangame.mbl.interceptor.checkreferer.CheckReferer;
import com.hangame.mbl.interceptor.expecthttpmethod.ExpectHttpMethod;
import com.nhncorp.lucy.security.permission.Permissions;
import com.nhncorp.lucy.web.dispatcher.ActionRedirect;

// TODO: Auto-generated Javadoc
/**
 * The Class FeedAction.
 *
 * @author NHN
 */
public class FeedAction extends SmartCommonBaseAction {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 7981766286723028921L;

	/* (non-Javadoc)
	 * @see com.nhncorp.lucy.web.actions.NoneContinuableActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		setFeedList();
		
		setAttribute("sidValid", getUser().get("idvalid"));
		
		return SUCCESS;
	}

	/**
	 * Write.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 */
	@Permissions(required = "login")
	@ExpectHttpMethod(method = "post")
	//	@CheckReferer
	public String write() throws Exception {
		int gameNo = NumberUtils.toInt(params.getString("gameNo"), 0); // 전체게임 일때. gameNo = 0으로 처리.
		String isMyFeed = StringUtils.defaultIfEmpty(params.getString("isMyFeed"), "N");
		String beforeSpace = StringUtils.defaultIfEmpty(params.getString("beforeSpace"), "");

		String userId = getUser().getUserId();
		int insertGameNo = params.getInt("insertGameNo");
		String content = params.getString("content");
		String attachFile = params.getString("attachFile");

		feedBO.writeFeed(userId, insertGameNo, content, attachFile);

		if (StringUtils.isEmpty(beforeSpace)) {
			return ActionRedirect.redirect("/feed/feed.nhn", "this", "GET", "gameNo=" + gameNo + "&isMyFeed=" + isMyFeed);
		} else {
			return ActionRedirect.redirect("/game/game.nhn", "this", "GET", "m=gameFeed&gameNo=" + gameNo + "&tabNo=3");
		}
	}

	/**
	 * Delete.
	 *
	 * @return the string
	 * @throws Exception the exception
	 */
	@Permissions(required = "login")
	@ExpectHttpMethod(method = "post")
	@CheckReferer
	public String delete() throws Exception {
		int gameNo = NumberUtils.toInt(params.getString("gameNo"), 0); // 전체게임 일때. gameNo = 0으로 처리.
		String isMyFeed = StringUtils.defaultIfEmpty(params.getString("isMyFeed"), "N");
		String beforeSpace = StringUtils.defaultIfEmpty(params.getString("beforeSpace"), "");
		int page = NumberUtils.toInt(params.getString("page"), 1);

		String userId = getUser().getUserId();
		int feedNo = params.getInt("feedNo");

		feedBO.deleteFeed(feedNo, userId);

		if (StringUtils.isEmpty(beforeSpace)) {
			return ActionRedirect.redirect("/feed/feed.nhn", "this", "GET", "gameNo=" + gameNo + "&isMyFeed=" + isMyFeed + "&page=" + page);
		} else {
			return ActionRedirect.redirect("/game/game.nhn", "this", "GET", "m=gameFeed&gameNo=" + gameNo + "&tabNo=3" + "&page=" + page);
		}
	}

}
